//
//  MainNavController.m
//  xxlaugh
//
//  Created by 周立贺 on 16/6/3.
//  Copyright © 2016年 周立贺. All rights reserved.
//

#import "MainNavController.h"

@implementation MainNavController

+(void)load{
    
    //设置全局的NavBar
    UINavigationBar * navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    navBar.titleTextAttributes = attrs;
    
}

- (void)viewDidLoad{
    
    [super viewDidLoad];

    //设置NavigationBar背景图片
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"home_background"] forBarMetrics:UIBarMetricsDefault];
}

#pragma mark -- 添加全局的push返回
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    //有值再去设置
    if (self.childViewControllers.count) {
        
        //创建按钮.
        UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //设置高亮和正常状态下的图片
        [backBtn setImage:[UIImage imageNamed:@"header_back_icon"] forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"header_back_icon_highlight"] forState:UIControlStateHighlighted];
        
        //设置尺寸
        [backBtn sizeToFit];
        
        //监听按钮的点击
        [backBtn addTarget:self action:@selector(popvc) forControlEvents:UIControlEventTouchDown];
        
        //添加到系统的item,(覆盖系统的item)
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
        
        //隐藏底部的tabBar
        viewController.hidesBottomBarWhenPushed = YES;

    }
    [super pushViewController:viewController animated:animated];
    
    //如果子控制器大于1,就设置Nav背景图片为白色
    if (self.childViewControllers.count > 1) {
        [viewController.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"TabBarBackImage"] forBarMetrics:UIBarMetricsDefault];
    }
    
}

- (void)popvc{
    
    [self popViewControllerAnimated:YES];
    
    //如果等于1为主界面,就设置Nav的背景图片为黄色的
    if (self.childViewControllers.count == 1) {
        //设置NavigationBar背景图片
        [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"home_background"] forBarMetrics:UIBarMetricsDefault];
    }
}

@end
